package tbs.littlebaby;

import Data.BasicData;
import Data.BitmapRequest;
import Data.StringDealer;
import UI.ExpandableListADP;
import UI.ImageRequestActivity;
import android.app.DatePickerDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.*;

import java.util.Calendar;

public class EditItemActivity extends ImageRequestActivity {
public  static final int Edit = 774;
public  static final int Add = 775;

String getTextFromEditText(int id)
{
  EditText text=  findViewById(id);
  return  text.getText().toString();
}
BasicData temp;
ImageView imageView;
int cate,type,mode;
String id;


    void SetDate(BasicData data)
{
    TextView pick=findViewById(R.id.edit_date);
    Calendar cal = Calendar.getInstance();
    cal.setTime(data.getDate());
    pick.setText(String.format("购买日期：%d年%d月%d日",cal.get(Calendar.YEAR),cal.get(Calendar.MONTH)+1,cal.get(Calendar.DAY_OF_MONTH)));
}
boolean ValueCheck()
{
  EditText text=  findViewById(R.id.edit_name);
  boolean a= text.getText().toString().length()>0;
  text=findViewById(R.id.edit_brand);
  boolean b=text.getText().toString().length()>0;
  text=  findViewById(R.id.edit_price);
  boolean c=isNumber(text.getText().toString());
  return a&&b&&c;
}
boolean isNumber(String text)
{
    try {
        Double.valueOf( text);
        return true;
    }catch (Exception e)
    {
        return false;
    }
}
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_item);
        temp=new BasicData();
        Button btn=findViewById(R.id.edit_save);
        imageView=findViewById(R.id.edit_image);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ValueCheck())
                {
                    temp=new BasicData(getString(cate),type,Double.valueOf(getTextFromEditText(R.id.edit_price)),getTextFromEditText(R.id.edit_name),getTextFromEditText(R.id.edit_brand),temp.getDate(),temp.getImage(),getTextFromEditText(R.id.edit_tag));
                    if(mode==Edit)
                    {
                        try {
                            int[] arr=EditItemActivity.this.getIntent().getIntArrayExtra("item");
                            if( MainActivity.save.Alter(id,temp))
                            {
                                ExpandableListADP.childs[arr[0]].set(arr[1],temp);
                                ItemList.refreashList(arr[0]);
                                Toast.makeText(EditItemActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
                            }
                            else
                                Toast.makeText(EditItemActivity.this,"修改失败",Toast.LENGTH_SHORT).show();

                        }catch (Exception ex)
                        {
                            Toast.makeText(EditItemActivity.this,"修改失败:"+ex.getMessage(),Toast.LENGTH_SHORT).show();
                        }

                    }
                    if(mode==Add)
                    {
                        try {
                            if(MainActivity.save.Add(temp))
                            {
                                ExpandableListADP.childs[type].add(temp);
                                ItemList.refreashList(type);
                                Toast.makeText(EditItemActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
                            }else
                                Toast.makeText(EditItemActivity.this,"添加失败",Toast.LENGTH_SHORT).show();

                        }catch (Exception ex)
                        {

                            Toast.makeText(EditItemActivity.this,"添加失败",Toast.LENGTH_SHORT).show();
                        }

                    }
                }
                else
                {
                    Toast.makeText(EditItemActivity.this,"品名，品牌，价格必填。",Toast.LENGTH_SHORT).show();
                }


            }
        });

        TextView pick=findViewById(R.id.edit_date);
        pick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int[] date=new int[]{2000,6,17};
                if(temp.getDate()!=null)
                {
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(temp.getDate());
                    date=new int[]{cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH)};
                }
                new DatePickerDialog(EditItemActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        temp.setDate(StringDealer.strToDateLong(String.format("%04d-%02d-%02d 00:00:00",year,month+1,dayOfMonth)));
                        SetDate(temp);
                    }
                },date[0],date[1],date[2]).show();
            }
        });

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BitmapRequest.GetImagePathFromUser(EditItemActivity.this);
            }
        });

        mode=getIntent().getIntExtra("type",-1);
        cate=getIntent().getIntExtra("pos",-1);
        type=getIntent().getIntExtra("child",-1);
        switch (mode)
        {
            case -1:
                Toast.makeText(this,"错误参数",Toast.LENGTH_SHORT).show();
                break;
            case Edit:
                int[] arr= this.getIntent().getIntArrayExtra("item");
                type=arr[0];
                temp= ExpandableListADP.childs[arr[0]].get(arr[1]);
                id=temp.getID();
                if(temp.getDate()!=null)
                {
                    pick.setTag(temp.getDate());
                    SetDate(temp);
                }
                else
                {
                    pick.setText("购买日期：不详");
                }
                if(temp.getImage()!=null)
                    imageView.setImageBitmap(BitmapRequest.ScaleImage(temp.getImage()));
               EditText tmp= findViewById(R.id.edit_brand);
               tmp.setText(temp.getBrand());
               tmp=findViewById(R.id.edit_name);
                tmp.setText(temp.getName());
                tmp=findViewById(R.id.edit_price);
                tmp.setText(String.valueOf( temp.getPrice()));
                if(temp.getTag()!=null)
                {
                    tmp=findViewById(R.id.edit_tag);
                    tmp.setText(temp.getTag());
                }
                break;
            case Add:


                break;
        }
    }

    @Override
    public void Handle(Message msg) {
        if(msg.what==1)
        {
                temp.setImage((Bitmap) msg.obj);
                imageView.setImageBitmap(BitmapRequest.ScaleImage(temp.getImage()));

        }
        else
        {
            Toast.makeText(EditItemActivity.this,"读取错误图片",Toast.LENGTH_SHORT).show();
        }
    }
}